<?php
// This file is part of YandexTranslate plugin for GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social.  If not, see <http://www.gnu.org/licenses/>.

defined('GNUSOCIAL') || die();

/**
 * @package     Plugin
 * @maintainer  kim <kim@surtdelcercle.cat>
 */
class YandexTranslatePlugin extends Plugin
{
    const PLUGIN_VERSION = '1.0.1';

    public static function settings($setting)
    {
        // Yandex API key see https://passport.yandex.com/
        $settings['api_key'] = '';

        // Language to translate notices (by default uses user's language)
        $settings['lang'] = common_language();

        // config.php settings override the settings in this file
        $configsettings = common_config('site', 'yandex') ?: [];
        foreach ($configsettings as $configsetting => $value) {
            $settings[$configsetting] = $value;
        }

        if (isset($settings[$setting])) {
            return $settings[$setting];
        } else {
            return false;
        }
    }

    public function initialize()
    {
        // show YandexTranslate link in the admin panel on the future
        //common_config_append('admin', 'panels', 'yandexadmin');
    }

    public function onEndShowScripts(Action $action)
    {
        //$action->script($this->path('jquery.min.js'));

        $script = "
		$(document).ready(function() {

		var apikey = '".static::settings('api_key')."'; //your Yandex apikey
		var lang = '".static::settings('lang')."';

		$('.ytranslate').on('click', function(e) {
			e.preventDefault();
			var id = $(this).attr('data-id');
			var status = $('#notice-'+id+' > .e-content');
			var text = status.html();
			var url = 'https://translate.yandex.net/api/v1.5/tr.json/translate?key='+apikey+'&text='+encodeURIComponent(text)+'&lang='+lang+'&format=html&options=1';
			$.ajax({
				 type : 'GET',
				 dataType : 'jsonp',
				 url : url,
				 callback: 'translate',
				 success: function(data){
					status.html(data.text[0]);
				 }
			});
		});
		});";

        $action->inlineScript($script);

        //write on log for debug
        //common_log(LOG_INFO, 'Message');

        return true;
    }

    public function onEndShowNoticeItem($item)
    {
        $item->out->element('a', ['href' => '#', 'class' => 'ytranslate', 'data-id' => $item->notice->id, 'title' => 'Translate'], 'Translate');
        return true;
    }

    public function onPluginVersion(array &$versions)
    {
        $versions[] = array('name' => 'Yandex Translate',
                            'version' => self::PLUGIN_VERSION,
                            'author' => 'Kim',
                            'homepage' => 'https://social.surtdelcercle.cat',
                            'rawdescription' =>
                            // TRANS: Plugin description.
                            _m('A simple script to translate notices.'));

        return true;
    }
}
